
BEGIN{
    GEMINI_RESPONSE_CONTENT = ""
    GEMINI_HAS_RESPONSE_CONTENT = 0
    GEMINI_RESPONSE_REASONING_CONTENT = ""
    GEMINI_RESPONSE_HAS_REASONING = 0

    KP_PART = "\"candidates\"" SUBSEP "\"1\"" SUBSEP "\"content\"" SUBSEP "\"parts\""
    Q2_1 = SUBSEP "\"1\""
}

function gemini_parse_response_data( text, obj,       _arr, _arrl, i, _current_kp, j, _part_l, _kp_part_idx, _kp_part_func){
    _arrl = json_split2tokenarr( _arr, text )
    for (i=1; i<=_arrl; ++i) {
        jiparse( obj, _arr[i] )
        if (( JITER_LEVEL != 1 ) || ( JITER_CURLEN <= 0) || ( _arr[i] ~ "^[,:]?$")) continue

        _current_kp = Q2_1 SUBSEP "\""JITER_CURLEN"\""
        if (( JITER_CURLEN == 1 ) && ( obj[ _current_kp, "\"error\"" ] != "" )) {
            o_error[ L ] = 1
            jmerge_force___value( o_error, Q2_1, obj, _current_kp )
            continue
        } else if ( JITER_CURLEN != 1 ) {
            jmerge_force___value( o_response, Q2_1 SUBSEP "\"1\"", o_response, Q2_1 SUBSEP "\""JITER_CURLEN"\"" )
        }

        _part_l = obj[ _current_kp, KP_PART L ]
        for (j=1; j<=_part_l; ++j){
            _kp_part_idx    = _current_kp SUBSEP KP_PART SUBSEP "\""j"\""
            gemini_record_response___text_content( obj, _kp_part_idx )
            gemini_record_response___tool_call( obj, _kp_part_idx )
        }
    }
}

function gemini_record_response___text_content( obj, kp,          item, is_thought ){
    item =  obj[ kp, "\"text\"" ]
    if ( chat_str_is_null(item) ) return
    item = juq(item)
    is_thought = obj[ kp, "\"thought\"" ]
    if ( is_thought == "true" ) {
        if ( GEMINI_RESPONSE_HAS_REASONING  == 0 ) {
            if ( IS_REASONING == true ) chat_record_str_to_drawfile( "---------- REASONING BEGIN ----------\n", DRAW_PREFIX )
        }
        GEMINI_RESPONSE_HAS_REASONING = 1
        GEMINI_RESPONSE_REASONING_CONTENT = GEMINI_RESPONSE_REASONING_CONTENT item
        if ( IS_REASONING == true ) chat_record_str_to_drawfile( item, DRAW_PREFIX )
        return
    } else if (GEMINI_RESPONSE_HAS_REASONING == 1){
        GEMINI_RESPONSE_HAS_REASONING = 0
        if ( IS_REASONING == true ) chat_record_str_to_drawfile( "\n---------- REASONING END ----------\n", DRAW_PREFIX )
    }

    GEMINI_RESPONSE_CONTENT = GEMINI_RESPONSE_CONTENT item
    chat_record_str_to_drawfile( item, DRAW_PREFIX )
}

function gemini_record_response___tool_call( obj, kp,        name, args, dir, idx){
    kp = kp SUBSEP "\"functionCall\""
    if ( obj[ kp L ] <= 0 ) return
    name = juq( obj[ kp, "\"name\"" ] )
    args = jstr0( obj, kp SUBSEP "\"args\"", " " )
    if ( name == "" ) return

    o_tool[ Q2_1 ] = "["
    jlist_put( o_tool, Q2_1, "{" )
    idx = o_tool[ Q2_1 L ]
    jdict_put( o_tool, Q2_1 SUBSEP "\""idx"\"", "\"index\"", idx)
    jdict_put( o_tool, Q2_1 SUBSEP "\""idx"\"", "\"name\"", jqu(name))
    jdict_put( o_tool, Q2_1 SUBSEP "\""idx"\"", "\"arguments\"", jqu(args))

    if ( GEMINI_CONTENT_DIR != "" ) {
        dir = (GEMINI_CONTENT_DIR "/function-call/" idx)
        mkdirp( dir )
        print name > (dir "/name")
        print args > (dir "/arg")
        if ( IS_ENACTNONE != true ) print "[FUNCTION-CALL] " idx >> XCMD_CHAT_ENACTALL_LOGFILE
    }

    fflush()
}

( IS_DEBUG ){ print $0 >> (GEMINI_CONTENT_DIR "/chat.running.yml"); }

($0 != ""){
    GEMINI_HAS_RESPONSE_CONTENT = 1
    gemini_parse_response_data( $0, obj )
}
